package cn.jy.nvr.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.controller.CollectionRecordController;
import cn.jy.nvr.controller.CommunRecordController;
import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.BloodDevelopment;
import cn.jy.nvr.domain.BloodDevelopmentJson;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.ProviderNurseLink;
import cn.jy.nvr.domain.UserSet;
import cn.jy.nvr.domain.VillageTown;
import cn.jy.nvr.domain.XY_BASE_RELDONNUR;
import cn.jy.nvr.mapper.BloodDevelopmentJsonMapper;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.UserSetMapper;
import cn.jy.nvr.mapper.XY_BASE_RELDONNURMapper;
import cn.jy.nvr.service.BloodDevelomentQuartzJob;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Service("BloodDevelomentQuartzJob")
public class BloodDevelomentQuartzJobImpl implements BloodDevelomentQuartzJob {
	@Autowired
	UserSetMapper userSetMapper;
	@Autowired
	XY_BASE_RELDONNURMapper xy_BASE_RELDONNURMapper;
	@Autowired
	ProviderBaseInfoMapper providerBaseInfoMapper;
	@Autowired
	BloodDevelopmentJsonMapper bloodDevelopmentJsonMapper;
	 private static String[] zone={"岷阳镇","蒲麻镇","西寨镇","梅川镇","西江镇","闾井镇","十里镇","茶埠镇","中寨镇","清水乡","马坞乡","寺沟乡","麻子川乡","秦许乡","禾驮乡","维新乡","申都乡","锁龙乡","宕昌县","合作市","临潭县","卓尼县","舟曲县","迭部县","玛曲县","碌曲县","夏河县"};
     private static List<UserSet> nurseList=null;
     private static  int temp=0;
     private static  int villageTownYgNums=0;
 	Logger logger=Logger.getLogger(BloodDevelomentQuartzJobImpl.class);
	@Override
	public void work() {
		//清空列表中护士的信息
		if(nurseList!=null){
			nurseList.clear();
		}
		//从数据库中拿出护士姓名
	    DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		nurseList = userSetMapper.getNurse();
		for (UserSet userSet : nurseList) {
			//拿到该护士所有链接的人员
			 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			List<XY_BASE_RELDONNUR> nurseLinkerList = xy_BASE_RELDONNURMapper.loadXY_BASE_RELDONNURByNurse(userSet.getPERSONID());
		//遍历所有链接人员拿到他们的基本信息分为义工和非义工的
			List<ProviderNurseLink> ygnurseLinkerBaseInfoList=new ArrayList<ProviderNurseLink>();
			List<ProviderNurseLink> fygnurseLinkerBaseInfoList=new ArrayList<ProviderNurseLink>();
			for (XY_BASE_RELDONNUR xy_BASE_RELDONNUR : nurseLinkerList) {
				 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				ProviderBaseInfo providerBaseInfo = providerBaseInfoMapper.getProviderBaseInfoByID(xy_BASE_RELDONNUR.getJYXM());
				if(providerBaseInfo!=null){
					Map<String, String> subMsgMap=new HashMap<String, String>();
					subMsgMap.put("address", providerBaseInfo.getDZ());
					subMsgMap.put("cardId", providerBaseInfo.getCARDID());
					subMsgMap.put("name", providerBaseInfo.getXM());
					subMsgMap.put("provideTimes", providerBaseInfo.getZJSJCS());
					subMsgMap.put("regTime", providerBaseInfo.getCREATERQ());
					subMsgMap.put("ID", providerBaseInfo.getID());
					ProviderNurseLink providerNurseLink =new ProviderNurseLink();
					providerNurseLink.setName(providerBaseInfo.getXM());
					providerNurseLink.setSubMsg(subMsgMap);
					providerNurseLink.setChildren(new ArrayList<ProviderNurseLink>());
					if(providerBaseInfo.getCWYGRQ()==null){
						providerNurseLink.getSubMsg().put("isVolunteer", "false");
						fygnurseLinkerBaseInfoList.add(providerNurseLink);
					}else{
						providerNurseLink.getSubMsg().put("isVolunteer", "true");
						ygnurseLinkerBaseInfoList.add(providerNurseLink);
					}
				}
			}
			for (ProviderNurseLink providerNurseLink : ygnurseLinkerBaseInfoList) {
				System.out.println(providerNurseLink+"义工");
			}
			
			List<ProviderNurseLink> ygnurseLinkerBaseInfoLists=new ArrayList<ProviderNurseLink>();
			if( ygnurseLinkerBaseInfoList!=null && ygnurseLinkerBaseInfoList.size()>0){
				for (ProviderNurseLink providerNurseLink : ygnurseLinkerBaseInfoList) {
					System.out.println(providerNurseLink+"-------------------------------------------------------");
					ProviderNurseLink ygLinkerinfo = getYgLinkerinfo(providerNurseLink);
					System.out.println(ygLinkerinfo+"-------------------------------------------------------");
					ygnurseLinkerBaseInfoLists.add(ygLinkerinfo);
				}
			}
			
			List<VillageTown>  villageTowns=new ArrayList<VillageTown>();
		//根据区域将所有的浆员进行分组
			for (int i = 0; i < zone.length; i++) {
				  VillageTown villageTown=new VillageTown();
				  villageTown.setName(zone[i]);
				  List<ProviderNurseLink> zoneLinkerLinks=new ArrayList<ProviderNurseLink>();
				  if( ygnurseLinkerBaseInfoLists!=null && ygnurseLinkerBaseInfoList.size()>0){
					  for (ProviderNurseLink providerNurseLink : ygnurseLinkerBaseInfoLists) {
						  if (providerNurseLink.getSubMsg().get("address").contains(zone[i])) {
							  zoneLinkerLinks.add(providerNurseLink);
							  villageTownYgNums++;
						  }
					  }
				  }
				  if (fygnurseLinkerBaseInfoList!=null && fygnurseLinkerBaseInfoList.size()>0) {
					  for (ProviderNurseLink fygproviderNurseLink : fygnurseLinkerBaseInfoList) {
						  if (fygproviderNurseLink.getSubMsg().get("address").contains(zone[i])) {
							  zoneLinkerLinks.add(fygproviderNurseLink);
						  }
					  }
				}
				 if(zoneLinkerLinks!=null && !zoneLinkerLinks.isEmpty()){
					 villageTown.setChildren(zoneLinkerLinks); 
					 Map<String, String>   infoMap =new HashMap<String, String>();
					      infoMap.put("villageTownNums", String.valueOf(zoneLinkerLinks.size()));
					      infoMap.put("villageTownYgNums", String.valueOf(villageTownYgNums));
					      villageTownYgNums=0;
				      villageTown.setSubMsg(infoMap);
				      villageTowns.add(villageTown);
				 }
			}
			
			BloodDevelopment bloodDevelopment=new BloodDevelopment();
			bloodDevelopment.setName(userSet.getPERSONNAME());
			 Map<String, String>   infoMap =new HashMap<String, String>();
			 infoMap.put("ygNums", String.valueOf(ygnurseLinkerBaseInfoList.size()));
			 infoMap.put("totalNums", String.valueOf(fygnurseLinkerBaseInfoList.size()+ygnurseLinkerBaseInfoList.size()));
		     infoMap.put("ygMangerNums", String.valueOf(temp));
			 
		     bloodDevelopment.setSubMsg(infoMap);
			 temp=0;
			 bloodDevelopment.setChildren(villageTowns);
			//存入数据库中
			ObjectMapper mapper = new ObjectMapper();
			String json = null;
			try {
				json = mapper.writeValueAsString( bloodDevelopment);
           //System.out.println(json);
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
			BloodDevelopmentJson bloodDevelopmentJson=new BloodDevelopmentJson();
			bloodDevelopmentJson.setName(userSet.getPERSONID());
			bloodDevelopmentJson.setJson(json);
			bloodDevelopmentJsonMapper.updateByNurse(bloodDevelopmentJson);
			
		}
		//遍历护士列表查找每个护士的对应的血源信息
		CollectionRecordController.map.clear();
	}
	

	public ProviderNurseLink   getYgLinkerinfo(ProviderNurseLink providerNurseLink) {
		 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<XY_BASE_RELDONNUR> allChildLinked = xy_BASE_RELDONNURMapper.getAllChildLinked(providerNurseLink.getSubMsg().get("ID"));
		List<ProviderNurseLink> providerNurseLinkchilds=new ArrayList<ProviderNurseLink>();
		//获取该义工下面的基本信息
		if(allChildLinked!=null && allChildLinked.size()>0){
			
			for (XY_BASE_RELDONNUR xy_BASE_RELDONNUR : allChildLinked) {
				 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				ProviderBaseInfo providerBaseInfo = providerBaseInfoMapper.getProviderBaseInfoByID(xy_BASE_RELDONNUR.getJYXM());
				if(providerBaseInfo!=null){
					Map<String, String> subMsgMap=new HashMap<String, String>();
					subMsgMap.put("address", providerBaseInfo.getDZ());
					subMsgMap.put("cardId", providerBaseInfo.getCARDID());
					subMsgMap.put("name", providerBaseInfo.getXM());
					subMsgMap.put("providerTimes", providerBaseInfo.getZJSJCS());
					subMsgMap.put("regTime", providerBaseInfo.getCREATERQ());
					ProviderNurseLink providerNurseLinkchild =new ProviderNurseLink();
					providerNurseLinkchild.setSubMsg(subMsgMap);
					providerNurseLinkchild.setName(providerBaseInfo.getXM());
					providerNurseLinkchild.setChildren(new ArrayList<ProviderNurseLink>());
					temp++;
					if(providerBaseInfo.getCWYGRQ()==null){
						providerNurseLink.getSubMsg().put("isVolunteer", "false");
						providerNurseLinkchilds.add(providerNurseLinkchild);
					}else{
						providerNurseLinkchild.getSubMsg().put("isVolunteer", "true");
						ProviderNurseLink Linkerinfo = getYgLinkerinfo(providerNurseLinkchild);
						providerNurseLinkchilds.add(Linkerinfo);
					}
				}
				providerNurseLink.setChildren(providerNurseLinkchilds);
				}
		}
	   return	providerNurseLink;
	}

}
